package com.ebelter.scaleblesdk.ble.model;


import com.ebelter.scaleblesdk.util.ULog;

/**
 * 闹钟数据bean
 *
 * @author fenghui
 */
public class AlarmClock {
    public static final String TAG = "AlarmClockBean";

    /**
     * 重复信息
     */
    private int[] weekRepeat;

    /**
     * 时间
     */
    private int hour;

    /**
     * 分钟
     */
    private int minute;

    /**
     * 秒
     */
    private int second = 0;
    /**
     * 闹钟是否打开
     */
    private boolean onOff = false;

    /**
     * 第几组，范围为0-4
     */
    private int no;
	/**
	 * 是否懒人模式
	 */
	private boolean isLoonState = false;
	
	
	public boolean isLoonState() {
		return isLoonState;
	}

	public void setLoonState(boolean isLoonState) {
		this.isLoonState = isLoonState;
	}

    public boolean isOnOff() {
        return onOff;
    }

    public void setOnOff(boolean onOff) {
        this.onOff = onOff;
    }

    public int[] getWeekRepeatNum() {
        return weekRepeat;
    }

    public int getWeekRepeat() {
        /*
            BIT7: 1,ON;0,off
			BIT6: 周日
			BIT5: 周六
			BIT4: 周五
			BIT3: 周四
			BIT2: 周三
			BIT1: 周二
			BIT0: 周一
		*/
        if (weekRepeat.length != 7) {
            ULog.e(TAG, "clock weekRepeat data is wrong.");
        }
        StringBuilder sb = new StringBuilder();
        sb.append(onOff ? 1 : 0);
//        for (int i : weekRepeat) {
//            sb.append(i);
//        }
        for (int i = weekRepeat.length - 1; i >= 0; i--) {
            sb.append(weekRepeat[i]);
        }
        ULog.i(TAG, "下发的闹钟信息 周期 = " + sb.toString());
        return Integer.parseInt(sb.toString(), 2);
    }

    public void setWeekRepeat(int[] weekRepeat) {
        this.weekRepeat = weekRepeat;
    }


    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public int getMinute() {
        return minute;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        if (no < 0 || no > 4) {
            no = 0;
        }
        this.no = no;
    }
	

    /**
     * 构建设置闹钟数据
     *
     * @return
     */
    public byte[] buildCmdBytes() {
        ULog.i(TAG, "构建第几组闹钟 =" + no);
        byte[] bytes = {
                (byte) 0xDB,
                (byte) 0x0A,
                0x03,
                (byte) no,
                0,
                0,
                0,
                0,
                (byte) hour,
                (byte) minute,
                (byte) second,
				(byte) getWeekRepeat(),
				isLoonState ? 0 : (byte)0xff
        };
        return bytes;
    }

    public int getSecond() {
        return second;
    }

    public void setSecond(int second) {
        this.second = second;
    }
	
	@Override
	public String toString() {
		
		return "获取到闹钟信息，编号：" + no + 
			  ",时间：" + hour + 
			  ",分钟：" + minute + 
			  ",是否打开：" + onOff + 
			  ",闹钟重复信息" + weekRepeat + ",是否懒人闹钟:" + isLoonState;
	}
}